package pl.andrzejag.unittests;

import static org.junit.Assert.*;

import java.util.Calendar;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import pl.mdziedzic.unittests.AUCTION_STATUS;
import pl.mdziedzic.unittests.AUCTION_TYPE;
import pl.mdziedzic.unittests.Auction;
import pl.mdziedzic.unittests.CreateAuctionRequest;
import pl.mdziedzic.unittests.User;
import pl.mdziedzic.unittests.services.RequestContext;

public class AuctionTestWithMock {
	User user;
	private RequestContext context;

	@Before
	public void SetUp() {
		user = new User("iHateJava");
		context = new RequestContext(user);
	}
	@Ignore()
	@Test
	public void shouldVerifyIfButNowAuctionIsCreatedCorrectly() {
		// given
		CreateAuctionRequest request = createBuyNowAuctionRequest();

		// when
		Auction auction = new Auction(context, request);

		// then
		assertThatBuyNowAuctionIsCreatedCorrectly(request, auction);

	}
	@Ignore()
	@Test
	public void shouldVerifyIfAuctionIsActivated() {
		// given
		CreateAuctionRequest request = createBuyNowAuctionRequest();
		Auction auction = new Auction(context, request);
		
		//podbijamy obecny czas
		Calendar now = context.getNow();
		now.add(Calendar.DATE, 1);
		now.add(Calendar.SECOND, 1);
		
		// when
		auction.activate();

		// then
		assertEquals(AUCTION_STATUS.ACTIVE, auction.getStatus());
		assertEquals(now, auction.getActivateDate());
	}

	private void assertThatBuyNowAuctionIsCreatedCorrectly(
			CreateAuctionRequest request, Auction auction) {
		assertNotNull(auction);
		assertEquals(request.getStartTime(), auction.getStartTime());
		assertEquals(request.getEndTime(), auction.getEndTime());
		assertEquals(request.getName(), auction.getName());
		assertEquals(request.getBuyNowPrice(), auction.getBuyNowPrice());
		assertFalse(auction.hasBids());
		assertEquals(AUCTION_TYPE.BUY_NOW, auction.getType());
		assertEquals(AUCTION_STATUS.NEW, auction.getStatus());
		assertNotNull(auction.getOwner());
	}

	private CreateAuctionRequest createBuyNowAuctionRequest() {
		String auctionName = "Buty";
		long buyNowPrice = 22;
		Calendar startTime = Calendar.getInstance();
		startTime.add(Calendar.DATE, 1);
		Calendar endTime = Calendar.getInstance();
		endTime.add(Calendar.DATE, 10);
		return CreateAuctionRequest.createBuyNowAuction(auctionName, startTime,
				endTime, buyNowPrice);
	}
}
